package com.itboye.ihomebank.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;

import java.util.Observable;
import java.util.Observer;

/**
 * 注册第2步（填写验证码）
 */
public class RegisterActivityTwo extends BaseOtherActivity implements OnClickListener, Observer {
    View v_statusbar;
    ImageView close_icon;//返回图片
    TextView add_shap_title_tv;//标题
    TextView res_two_phone;//手机号
    EditText res_code;//验证码
    TextView res_two_time;//验证码重新发送
    TextView res_two_ok;//下一步

    private TimeCount time;//倒计时

    private Intent intent;

    private String phone;//接上页面传来的手机号
    private String yzm;//接上页面传来的验证码（暂时收不到信息，验证码从上个页面传过来）

    private UserPresenter userPresenter;


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon://返回
                finish();

                break;
            case R.id.res_two_time://重新发送验证码
                getyzm();
                time.start();

                break;
            case R.id.res_two_ok://下一步
                VerificationYZM();


                break;

            default:
                break;
        }

    }

    /**
     * 获取验证码
     */
    private void getyzm() {
        userPresenter = new UserPresenter(this);
        userPresenter.send(phone, "1");
    }

    /**
     * 验证验证码是否正确
     */
    private void VerificationYZM() {
        if (res_code.getText().toString().trim().equals("")) {
            ByAlert.alert("请输入验证码");
            return;
        }

        userPresenter = new UserPresenter(this);
        userPresenter.verification(phone, "1", res_code.getText().toString());
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (!entity.getCode().equals(0)) {
                if (entity.getEventType() == UserPresenter.verification_success) {
                    String bean = (String) entity.getData();

                    ByAlert.alert(bean);
                    Intent intent = new Intent(RegisterActivityTwo.this, RegisterActivityThree.class);
                    intent.putExtra("phone", phone);
                    intent.putExtra("yzm", res_code.getText().toString());
                    startActivity(intent);
                    RegisterActivityTwo.this.finish();

                }
                if (entity.getEventType() == UserPresenter.verification_fail) {
                    ByAlert.alert(entity.getData());
                }
                if (entity.getEventType() == UserPresenter.send_success) {
                    String bean = (String) entity.getData();
                    if (bean != null) {
                        ByAlert.alert("验证码发送成功");
                    } else {
                        ByAlert.alert("失败");
                    }
                }
                if (entity.getEventType() == UserPresenter.send_fail) {
                    ByAlert.alert("验证码发送失败");
                }
            }
        }

    }

    @Override
    protected int layout_id() {
        // TODO Auto-generated method stub
        return R.layout.activity_register_two;
    }

    @Override
    protected void onInitController() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onInit() {
        // TODO Auto-generated method stub
//        add_shap_title_tv.setHint("填写验证码");

        setStatusBarHeight(v_statusbar);
        time = new TimeCount(60000, 1000, res_two_time);// 构造CountDownTimer对象
        time.start();

        intent = super.getIntent();
        phone = intent.getStringExtra("phone");
        yzm = intent.getStringExtra("yzm");

        res_two_phone.setText(phone);

//        res_code.setText(yzm);

    }

    @Override
    protected void onReadIntent(Bundle bundle) {
        // TODO Auto-generated method stub

    }


}
